﻿using System.Collections.Generic;
using System.Linq;
using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;
namespace Wedonek.RpcStore.Gateway.Service
{
        internal class AutoTaskLogService : IAutoTaskLogService
        {
                private readonly IAutoTaskLogCollect _TaskLog;
                private readonly IAutoTaskItemCollect _TaskItem;
                private readonly IErrorLangMsgCollect _ErrorMsg;
                private readonly IErrorCollect _Error;
                public AutoTaskLogService (IAutoTaskLogCollect taskLog,
                        IErrorCollect error,
                        IAutoTaskItemCollect taskItem,
                        IErrorLangMsgCollect errorMsg)
                {
                        this._Error = error;
                        this._TaskItem = taskItem;
                        this._ErrorMsg = errorMsg;
                        this._TaskLog = taskLog;
                }

                private Dictionary<long, string> _GetErrors (long[] errorId)
                {
                        Dictionary<long, string> errors = new Dictionary<long, string> ();
                        if (errorId.Length > 0)
                        {
                                ErrorDatum[] er = this._Error.Gets (errorId);
                                er.ForEach (a => errors.Add (a.Id, a.ErrorCode));
                                errorId = er.Convert (c => c.IsPerfect, c => c.Id);
                                if (errorId.Length > 0)
                                {
                                        ErrorLang[] langs = this._ErrorMsg.GetErrorMsg (errorId);
                                        langs.ForEach (a => errors[a.ErrorId] = a.Msg);
                                }
                        }
                        return errors;
                }
                public AutoTaskLog[] Query (TaskLogQuery query, IBasicPage paging, out long count)
                {
                        TaskLog[] logs = this._TaskLog.Query (query, paging, out count);
                        if (logs.Length == 0)
                        {
                                return null;
                        }
                        TaskItemTitle[] items = this._TaskItem.GetTitles (logs.Distinct (a => a.ItemId));
                        long[] errorId = logs.Distinct (a => a.ErrorCode != 0, a => a.ErrorCode);
                        Dictionary<long, string> errors = this._GetErrors (errorId);
                        return logs.ConvertMap<TaskLog, AutoTaskLog> ((a, b) =>
                        {
                                string title = items.Find (c => c.Id == a.ItemId, c => c.ItemTitle);
                                b.ItemName = title ?? "已失效";
                                if (a.ErrorCode != 0)
                                {
                                        b.Error = errors[a.ErrorCode];
                                }
                                return b;
                        });
                }
        }
}
